package cn.com.recode.c去除多余的临时变量;

import java.util.Enumeration;
import java.util.Vector;

public class Customer {

    private String _name;

    private Vector _rentals = new Vector();

    public Customer(String name) {
        this._name = name;
    }

    public void addRental(Rental rental) {
        _rentals.add(rental);
    }

    public String getName() {
        return _name;
    }

    /**
     * 1.thisAmout接受each.getCharge的值后并没任何该表，尽量除去这一类临时变量
     *   临时变量往往引发问题，他们会导致大量参数被传来传去，其实完全没必要。
     * @return
     */
    public String statement() {
        Enumeration rentals = _rentals.elements();
        String result = "Rental  Record for " + getName() + "\n";
        while (rentals.hasMoreElements()) {
            Rental each = (Rental) rentals.nextElement();
            result += "\t" + each.getMovie().getTitle() + "\t" +
                    String.valueOf(each.getCharge()) + "\n";
        }
        result += "Amout owed is " + String.valueOf(getTotalCharge()) + "\n";
        result += "You earned " + String.valueOf(getTotalFrenquentRenterPoints()) + " frequent renter points";
        return result;
    }


    private double getTotalCharge() {
        double result = 0;
        Enumeration rentals = _rentals.elements();
        while (rentals.hasMoreElements()) {
            Rental rental = (Rental) rentals.nextElement();
            result += rental.getCharge();
        }
        return result;
    }

    private double getTotalFrenquentRenterPoints() {
        double frenquentRenterPoints = 0;
        Enumeration rentals = _rentals.elements();
        while (rentals.hasMoreElements()) {
            Rental rental = (Rental) rentals.nextElement();
            frenquentRenterPoints += rental.getFrenquentRenterPoints();
        }
        return frenquentRenterPoints;
    }



}